// Modules to control application life and create native browser window
const {app, BrowserWindow,ipcMain } = require('electron')
const path = require('path')

function createWindow () {
  // 隐藏菜单栏
  // 创建浏览器窗口
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      webSecurity: false
    }
  })
  mainWindow.setMenu(null)
  // 并且为你的应用加载index.html
  mainWindow.loadFile('index.html')

  // 打开开发者工具
  mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(() => {
  const {session} = require('electron');
  const xxx_filter = {
      urls: ["*://*/*"]
    }
    session.defaultSession.webRequest.onBeforeSendHeaders(xxx_filter, (details, callback)=> {
      details.requestHeaders['sec-ch-ua'] = '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"'
      details.requestHeaders['sec-ch-ua-mobile'] = '?0'
      details.requestHeaders['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
                callback({requestHeaders: details.requestHeaders});
    })
  createWindow()
  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.commandLine.appendSwitch('--ignore-certificate-errors', 'true')
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
let lineWindow = null
ipcMain.handle('go-liveline', (event, ...args) => {
  // 隐藏菜单栏
  // 创建浏览器窗口
  if (lineWindow != null) {
    try {
      lineWindow.close()
    } catch (error) {
    }
  }
  lineWindow = new BrowserWindow({
    width: 300,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      webSecurity: false
    }
  })
  lineWindow.setMenu(null)
  // 并且为你的应用加载index.html
  lineWindow.loadFile('liveline.html')
})

let jsonWindow = null
ipcMain.handle('go-jsonnice', (event, ...args) => {
  // 隐藏菜单栏
  // 创建浏览器窗口
  if (jsonWindow != null) {
    try {
      jsonWindow.close()
    } catch (error) {
    }
  }
  jsonWindow = new BrowserWindow({
    width: 600,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      webSecurity: false
    }
  })
  jsonWindow.setMenu(null)
  // 打开开发者工具
  //jsonWindow.webContents.openDevTools()
  // 并且为你的应用加载index.html
  jsonWindow.loadFile('jsonnice.html')
  
})

let xmlWindow = null
ipcMain.handle('go-xmlnice', (event, ...args) => {
  // 隐藏菜单栏
  // 创建浏览器窗口
  if (xmlWindow != null) {
    try {
      xmlWindow.close()
    } catch (error) {
    }
  }
  xmlWindow = new BrowserWindow({
    width: 600,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      webSecurity: false
    }
  })
  xmlWindow.setMenu(null)
  // 打开开发者工具
  //xmlWindow.webContents.openDevTools()
  // 并且为你的应用加载index.html
  xmlWindow.loadFile('xmlnice.html')
  
})

let abWindow = null
ipcMain.handle('go-avbv', (event, ...args) => {
  // 隐藏菜单栏
  // 创建浏览器窗口
  if (abWindow != null) {
    try {
      abWindow.close()
    } catch (error) {
    }
  }
  abWindow = new BrowserWindow({
    width: 300,
    height: 200,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      webSecurity: false
    }
  })
  abWindow.setMenu(null)
  // 打开开发者工具
  //abWindow.webContents.openDevTools()
  // 并且为你的应用加载index.html
  abWindow.loadFile('avbv.html')
  
})